﻿using System.Collections.Generic;
using System.Diagnostics;
using GameStructure.Contracts;

namespace GameStructure.GaneState
{
    public class StateSystem
    {
        readonly Dictionary<string, IGameObject> _stateStore = new Dictionary<string, IGameObject>();
        private IGameObject _currentState;

        public Dictionary<string, IGameObject> States { get { return _stateStore; } }
        public IGameObject CurrentState { get { return _currentState; } }

        public void Update(double elapsedTime)
        {
            if (_currentState == null)
            {
                return; // nothing to update
            }

            _currentState.Update(elapsedTime);
        }

        public void Render()
        {
            if (_currentState == null)
            {
                return; // nothing to render
            }

            _currentState.Render();
        }

        public void AddState(string stateId, IGameObject state)
        {
            Debug.Assert(Exists(stateId) == false);
            _stateStore.Add(stateId, state);
        }

        public void ChangeState(string stateId)
        {
            Debug.Assert(Exists(stateId));
            _currentState = _stateStore[stateId];
        }

        public bool Exists(string stateId)
        {
            return _stateStore.ContainsKey(stateId);
        }
    }
}
